<?php
namespace app\index\controller;
use app\common\service\CateService;
use think\Db;

class Cate extends Common
{


    public function index()
    {
        $id = $this->request->param('cid');
        $info = Db::name('cate')->where('id',$id)->find();
		
		$info['navigation'][] = ['id'=>$info['id'],'title'=>$info['title']];
		if(!empty($info['parent_id'])){
			$parentInfo = Db::name('cate')->where('id',$info['parent_id'])->field('title,is_hide_time,type,id,parent_id')->find();
			array_unshift($info['navigation'],['id'=>$parentInfo['id'],'title'=>$parentInfo['title']]);
			if(!empty($parentInfo['parent_id'])){
				$rootInfo = Db::name('cate')->where('id',$parentInfo['parent_id'])->field('title,is_hide_time,type,id,parent_id')->find();
				array_unshift($info['navigation'],['id'=>$rootInfo['id'],'title'=>$rootInfo['title']]);
			}
		}
		
        $this->assign('info',$info);
		//dump($info['type']);die;
        if($info['type'] == 1){
			$cate_tree = CateService::cate_tree($info,$id);
			$this->assign('cate_tree',$cate_tree);
			
			$cid = input('cid');
			$all_childis = getAllChildId($cid);
			if($all_childis){
				array_push($all_childis,intval($cid));
			}else{
				$all_childis[] = intval($cid);
			}
			
			if(($info['parent_id'] == 0)){
				$cids = $cate_tree['sub'][0]['id'];
			}else{
				$parent_id = Db::name('cate')->where('id',$info['parent_id'])->value('parent_id');
				if($parent_id == 0){
					$cids= $cid;
				}else{
					$cids[] = $info['parent_id'];
					array_push($cids,$cid);
				}
			}
			$cate_rootinfo = Db::name('cate')->where('id',$cate_tree['id'])->find();
            return $this->fetch('case',['cid'=>$cids,'cate_rootinfo'=>$cate_rootinfo,'all_childis'=>$all_childis]);
			
        }elseif($info['type'] == 2){
			
			$cate_tree = CateService::cate_tree($info,$id);
			$this->assign('cate_tree',$cate_tree);
			
			$cid = input('cid');
			$all_childis = getAllChildId($cid);
			if($all_childis){
				array_push($all_childis,intval($cid));
			}else{
				$all_childis[] = intval($cid);
			}
			//dump($all_childis);die;
			//$cid = ($info['parent_id'] == 0)?$cate_tree['sub'][0]['id']:$cid;
			if(($info['parent_id'] == 0)){
				$cid = $cate_tree['sub'][0]['id'];
				//$cids = Db::name('cate')->where('parent_id',$cid)->column('id');
				//array_push($cids,$cid);
				$cids= $cid;
				//dump($cids);die;
			}else{
				$parent_id = Db::name('cate')->where('id',$info['parent_id'])->value('parent_id');
				if($parent_id == 0){
					$cids= $cid;
				}else{
					$cids = $info['parent_id'];
				}
			}
			//dump($cate_tree);
			$cate_rootinfo = Db::name('cate')->where('id',$cate_tree['id'])->find();
            return $this->fetch('list_pro',['cid'=>$cids,'cate_rootinfo'=>$cate_rootinfo,'all_childis'=>$all_childis]);
        }elseif($info['type'] == 3){
			$cate_tree = Db::name('cate')->where('parent_id',$id)->whereNull('delete_time')->order('sort asc,id asc')->select();
			$this->assign('cate_tree',$cate_tree);
			return $this->fetch('support');
		}elseif($info['type'] == 4){
			$cate_tree = Db::name('cate')->where('parent_id',$id)->whereNull('delete_time')->order('sort asc,id asc')->select();
			$this->assign('cate_tree',$cate_tree);
			return $this->fetch('hezuo');
		}elseif($info['type'] == 5){
			$cate_tree = Db::name('cate')->where('parent_id',$id)->whereNull('delete_time')->order('sort asc,id asc')->select();
			foreach($cate_tree as $k=>$v){
				$cate_tree[$k]['sub'] = Db::name('cate')->where('parent_id',$v['id'])->whereNull('delete_time')->order('sort asc,id asc')->select();
			}
			$this->assign('cate_tree',$cate_tree);
			//dump($cate_tree);die;
			
			return $this->fetch('about');
		}elseif($info['type'] == 10){
			
			$article_list = Db::name('article')->where('cate_id',$id)->paginate(12,false);
			return $this->fetch('news',['article_list'=>$article_list,'cid'=>$id]);
		}else{
            return $this->fetch('case');
        }

    }

}
